//
//  TQYFMDBManager.m
//  TQY
//
//  Created by 甘世清 on 2024/10/31.
//

#import "TQYFMDBManager.h"
#import "IMMessageManager.h"
@implementation TQYFMDBManager
+(void)SQ_creattable{
    //创建聊天类
    [[JQFMDB shareDatabase] jq_alterTable:[TQYFMDBManager SQ_getFriendListName] dicOrModel:[IMListModel new]];
}
 

//聊天列表读取
+(NSArray*)SQ_IMAcquire{
    JQFMDB *db = [JQFMDB shareDatabase];
    NSArray *personArr = [db jq_lookupTable:[TQYFMDBManager SQ_getFriendListName] dicOrModel:[IMListModel class] whereFormat:@"order by sendTime desc"];
    return personArr;
}

+(BOOL)SQ_addListModel:(IMListModel*)model{
    JQFMDB *db = [JQFMDB shareDatabase];
    if ([db jq_isExistTable:[TQYFMDBManager SQ_getFriendListName]]) { //有表
        NSString * string = [NSString stringWithFormat:@"where roomId = '%@'",model.roomId];
        [db jq_alterTable:[TQYFMDBManager SQ_getFriendListName] dicOrModel:[IMListModel class]];//更新模型数据key
    [db jq_deleteTable:[TQYFMDBManager SQ_getFriendListName] whereFormat:string];
    }else {
        [db jq_createTable:[TQYFMDBManager SQ_getFriendListName] dicOrModel:[IMListModel class]];
    }
    
    BOOL isok =   [db jq_insertTable:[TQYFMDBManager SQ_getFriendListName] dicOrModel:model];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"IMListModel" object:model];
    return isok;
}
/// 设置指定房间未读数为0
/// - Parameter roomID: 房间
+(BOOL)SQ_updateModel:(NSString*)roomID{
    NSString * string = [NSString stringWithFormat:@"where roomId = '%@'",roomID];
    JQFMDB *db = [JQFMDB shareDatabase];
    BOOL  isok = [db jq_updateTable:[TQYFMDBManager SQ_getFriendListName] dicOrModel:@{@"messageCount":@"0"} whereFormat:string];
    return isok;
}

/// 根据查询的房间id返回一个房间模型
/// - Parameter roomID: 房间id
+(IMListModel*)SQ_roomIDGetIMListModel:(NSString*)roomID{
    NSString * string = [NSString stringWithFormat:@"where roomId = '%@'",roomID];
    JQFMDB *db = [JQFMDB shareDatabase];
    NSArray * moarray = [db jq_lookupTable:[TQYFMDBManager SQ_getFriendListName] dicOrModel:[IMListModel class] whereFormat:string];
    if (moarray.count > 0) {
        return moarray.firstObject;
    }
    return [IMListModel new];
}
/// 根据房间id删除整个数据
/// - Parameter roomID: 房间id
+(BOOL)SQ_deletet:(NSString*)roomID{
    IMListModel * mo = [TQYFMDBManager SQ_roomIDGetIMListModel:roomID];
    if (![mo.roomId isNotEmpty]) {
        return YES;
    }
    JQFMDB *db = [JQFMDB shareDatabase];
    if ([db jq_isExistTable:[TQYFMDBManager SQ_getFriendListName]]) {
        //删除里面的模型数据
        NSString * name = [NSString stringWithFormat:@"IM%@",roomID];
        if ([db jq_isExistTable:name]) {
            [db jq_deleteTable:name];
        }
        //删除外面的列表
        NSString * string = [NSString stringWithFormat:@"where roomId = '%@'",roomID];
      BOOL isok =  [db jq_deleteTable:[TQYFMDBManager SQ_getFriendListName] whereFormat:string];
        return isok;
    }
    return  NO;
}

/// 获取未读数多少
+(NSString*)SQ_getMessageCount{
    NSInteger messageCount = 0;
    JQFMDB *db = [JQFMDB shareDatabase];
    NSString * string = [NSString stringWithFormat:@"where messageCount > '0'"];
    
    NSArray * moarray = [db jq_lookupTable:[TQYFMDBManager SQ_getFriendListName] dicOrModel:[IMListModel class] whereFormat:string];
    if (moarray.count > 0) {
        for (IMListModel * mo in moarray) {
            messageCount += mo.messageCount;
        }
    } 
    return [NSString stringWithFormat:@"%ld",(long)messageCount];
}

/// 存储消息列表接口
/// - Parameter model: 消息模型
+(BOOL)SQ_addListIMMessage:(IMMessageModel*)model{
    
    //先查询是否存在列表immodel
    NSString  * roomID = [NSString stringWithFormat:@"%@_%@",[UserModel share].userid,model.toUser];//房间id
    JQFMDB *db = [JQFMDB shareDatabase];
    NSString * name = [NSString stringWithFormat:@"IM%@",roomID];
    if ([db jq_isExistTable:name]) {
        [db jq_alterTable:name dicOrModel:[IMMessageModel class]];
        if ([model.messageId isNotEmpty]) {
        NSString * string = [NSString stringWithFormat:@"where messageId = '%@'",model.messageId];//删除相同消息
        [db jq_deleteTable:name whereFormat:string];
        }
       if ([model.pid isNotEmpty]){
            NSString * pid = [NSString stringWithFormat:@"where pid = '%@'",model.pid];//删除相同消息
            [db jq_deleteTable:name whereFormat:pid];
        }
    } else {
      [db jq_createTable:name dicOrModel:[IMMessageModel class]];
    }
    BOOL isok =  [db jq_insertTable:name dicOrModel:model];
    DTLog(@"消息存储状态--- %d --- %@",isok,[NSString SQ_currentTime]);
    if (isok && !model.isSend) {
        //存储成功就发送通知
    AudioServicesPlaySystemSound(kSystemSoundID_Vibrate); //加个震动
     [[NSNotificationCenter defaultCenter] postNotificationName:@"IMMessageModel" object:[model mj_JSONObject]];
        [IMMessageManager SQ_showMessage:model];
    } 
    return  isok;
}

+(NSArray*)SQ_UserAcquireID:(NSString*)roomId size:(NSInteger)size page:(NSInteger)page{
//    NSInteger offset = (page - 1) * size;
    JQFMDB *db = [JQFMDB shareDatabase];
//    NSString *query = [NSString stringWithFormat:@"ORDER BY messageId DESC LIMIT %ld OFFSET %ld", (long)size, (long)offset];
    NSString * query = [NSString stringWithFormat:@"order by sendTime desc, messageId desc limit %ld, %ld", (long)page,(long)size + 30];
    NSArray *personArr = [db jq_lookupTable:[NSString stringWithFormat:@"IM%@",roomId] dicOrModel:[IMMessageModel class] whereFormat:query];
    return personArr;
}

/// 根据消息id删除单个数据
/// - Parameter roomID: 房间id
+(BOOL)SQ_deletetMessage:(IMMessageModel*)model{
    JQFMDB *db = [JQFMDB shareDatabase];
    NSString  * roomID = [NSString stringWithFormat:@"%@_%@",[UserModel share].userid,model.toUser];//房间id
    NSString * name = [NSString stringWithFormat:@"IM%@",roomID];
    if ([db jq_isExistTable:name]) {
        [db jq_alterTable:name dicOrModel:[IMMessageModel class]];
        NSString * string = [NSString stringWithFormat:@"where messageId = '%@'",model.messageId];//删除相同消息
        BOOL isok = [db jq_deleteTable:name whereFormat:string];
        return isok;
    }
    return YES;
}

 
/// 根据消息id来判断是否该消息已存储
/// - Parameters:
///   - messageId: 消息id
///   - roomId: 房间id
+(BOOL)SQ_messageModel:(NSString*)messageId roomId:(NSString*)roomId{
    NSString * string = [NSString stringWithFormat:@"where messageId = %@",messageId];
    JQFMDB *db = [JQFMDB shareDatabase];
    NSArray * moarray = [db jq_lookupTable:[NSString stringWithFormat:@"IM%@",roomId] dicOrModel:[IMMessageModel class] whereFormat:string];
    if (moarray.count > 0) {
        return YES;
    }
    return NO;
}
//当前pid
+(NSString*)SQ_getpid{
    NSString * key = [NSString stringWithFormat:@"pid_%@",[UserModel share].userid];
    NSString * name =  [Manager SQ_stringForKey:key];
    if ([name isNotEmpty]) {
        int pid = [name intValue]+1;
        [Manager SQ_SetObject:[NSString stringWithFormat:@"%d",pid] FormKey:key];
        return [NSString stringWithFormat:@"%d",pid];
    } else {
        int pid = 1;
        [Manager SQ_SetObject:[NSString stringWithFormat:@"%d",pid] FormKey:key];
        return [NSString stringWithFormat:@"%d",pid];;
    }
}
+(NSString*)SQ_getFriendListName{
       NSString * name = [NSString stringWithFormat:@"FriendList_%@",[UserModel share].userid];
    return name;
}
//最小id存储key
+(NSString*)SQ_getminIDName{
    return  [NSString stringWithFormat:@"IM_Max_%@",[UserModel share].userid];
}
@end
